/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package simhya.matheval;
import java.util.ArrayList;

/**
 *
 * @author Luca
 */
public class Expression {
    private ExpressionNode root;
    private int id;
    private Evaluator eval;

    public Expression(ExpressionNode root, Evaluator eval) {
        this.root = root;
        this.id = -1;
        this.eval = eval;
    }

    /**
     * gets the ID code of the expression (can be used to retrieve it from the evaluator)
     * @return
     */
    public int getId() {
        return id;
    }

    /**
     * sets the id code of the expression. To be used by the evaluator.
     * @param id
     */
    void setId(int id) {
        this.id = id;
    }


    
    
    /**
     *
     * @return a list of variables ID of the function
     */
    public ArrayList<Integer> getListOfVariables() {
        ArrayList<Integer> list = new ArrayList<Integer>();
        root.getListOfVariables(list);
        return list;
    }

    public boolean isLogicalExpression() {
        return root.isLogicalExpression();
    }

    public double computeValue() {
        root.computeValue();
        return root.value;
    }
    public double computeValue(SymbolArray varReference){
        root.computeValue(varReference);
        return root.value;
    }
    public double computeValueFast(){
        root.computeValue(eval.evaluationCode);
        return root.value;
    }
    public double computeValueFast(SymbolArray varReference){
        root.computeValue(varReference,eval.evaluationCode);
        return root.value;
    }
    public boolean isRandom() {
        return root.random;
    }
    @Override
    public String toString() {
        return root.getExpressionString();
    }




}
